package com.zy.openchat.core.model.video;

import android.os.Parcel;
import android.os.Parcelable;

import com.zy.openchat.Constants;
import com.zy.openchat.core.model.file.FileInfo;
import com.zy.openchat.core.model.photo.ThumbnailInfo;

import org.apache.commons.lang3.StringUtils;

import java.io.File;

public class VideoInfo implements Parcelable {
    //file_name : "BA1E621DF2C34C3092D9410FAAB32DE3.mp4"
    public String file_name;
    public int duration;
    public float width;
    public float height;
    public ThumbnailInfo thumbnail;
    public FileInfo video;
    public FileInfo animation;

    public boolean isVideo(){
        return video!=null;
    }

    public long totalSize() {
        if (video != null)
            return video.expected_size;
        return 0;
    }




    public long donwloadSize() {
        if (video != null && video.local != null)
            return video.local.downloaded_size;
        return 0;
    }


    public long totalAnimationSize() {
        if (animation != null)
            return animation.expected_size;
        return 0;
    }

    public long donwloadAnimationSize() {
        if (animation != null && animation.local != null)
            return animation.local.downloaded_size;
        return 0;
    }

    public long donwloadAnimationStart() {
        if (animation != null && animation.local != null)
            return animation.local.download_offset+animation.local.downloaded_prefix_size;
        return 0;
    }

    public long getVideoFileId() {
        if (video != null) {
            return video.id;
        }
        return 0;
    }

    public String getRemoteVideoFileId() {
        if (video != null&&video.remote!=null) {
            return video.remote.id;
        }
        return "";
    }

    public String getRemoteAnimationFileId() {
        if (animation != null&&animation.remote!=null) {
            return animation.remote.id;
        }
        return "";
    }

    public long downloadedStart(){
        if (video != null) {
            return video.downloadedStart();
        }
        return 0;
    }

    public long getAnimationFileId() {
        if (animation != null) {
            return animation.id;
        }
        return 0;
    }

    public String localVideoPath() {
        if (video != null && video.local != null && video.local.isExist()) {
            return video.local.path;
        }
        //本地路径，根据file_name获取
        if (!StringUtils.isEmpty(file_name)) {
            try {
                String path = Constants.userVideoPath() + file_name;
                File f = new File(path);
                if (f.exists()) {
                    return path;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public String localAnimationPath() {
        if (animation != null && animation.local != null && animation.local.isExist()) {
            return animation.local.path;
        }
        //本地路径，根据file_name获取
        if (!StringUtils.isEmpty(file_name)) {
            try {
                String path = Constants.userVideoPath() + file_name;
                File f = new File(path);
                if (f.exists()) {
                    return path;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public boolean isVideoDownloadComplete(){
        if(isVideoDownloaded()){
            return video.local!=null&&video.local.is_downloading_completed;
        }else{
            return false;
        }
    }

    public boolean isAnimationDownloadComplete(){
        if(animation.local!=null){
            return animation.local.is_downloading_completed;
        }else{
            return false;
        }
    }

    public boolean isVideoDownloaded() {
        if (video != null && video.local != null && video.local.isExist()) {
            return true;
        }
        //本地路径，根据file_name判断
        if (!StringUtils.isEmpty(file_name)) {
            try {
                String path = Constants.userVideoPath() + file_name;
                File f = new File(path);
                if (f.exists()) {
                    return true;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    public boolean isAnimationDownloaded() {
        if (animation != null && animation.local != null && animation.local.isExist()) {
            return true;
        }
        //本地路径，根据file_name判断
        if (!StringUtils.isEmpty(file_name)) {
            try {
                String path = Constants.userVideoPath() + file_name;
                File f = new File(path);
                if (f.exists()) {
                    return true;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    public void updateVideoFile(FileInfo file) {
        if (video != null) {
            video = file;
        }
    }

    public void updateAnimationFile(FileInfo file) {
        if (animation != null) {
            animation = file;
        }
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.file_name);
        dest.writeInt(this.duration);
        dest.writeFloat(this.width);
        dest.writeFloat(this.height);
        dest.writeParcelable(this.thumbnail, flags);
        dest.writeParcelable(this.video, flags);
        dest.writeParcelable(this.animation,flags);
    }

    public VideoInfo() {
    }

    protected VideoInfo(Parcel in) {
        this.file_name = in.readString();
        this.duration = in.readInt();
        this.width = in.readFloat();
        this.height = in.readFloat();
        this.thumbnail = in.readParcelable(ThumbnailInfo.class.getClassLoader());
        this.video = in.readParcelable(FileInfo.class.getClassLoader());
        this.animation = in.readParcelable(FileInfo.class.getClassLoader());
    }

    public static final Creator<VideoInfo> CREATOR = new Creator<VideoInfo>() {
        @Override
        public VideoInfo createFromParcel(Parcel source) {
            return new VideoInfo(source);
        }

        @Override
        public VideoInfo[] newArray(int size) {
            return new VideoInfo[size];
        }
    };
}
